package com.leetroll.media.service;


import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.*;

@Component
@Slf4j
public class JobExecutor {
    private final ExecutorService executor = Executors.newFixedThreadPool(10);
    private final List<FutureTask<?>> futureTasks = new ArrayList<>();

    public void submit(Callable<?> callable) {
        FutureTask<?> task = new FutureTask<>(callable);
        futureTasks.add(task);
        executor.submit(task);
    }

    public void await() throws ExecutionException, InterruptedException {
        for (FutureTask<?> task : futureTasks) {
            task.get();
        }
        log.info("finished all: {}", futureTasks.size());
        futureTasks.clear();
    }
}
